#pragma once
#include<iostream>
#include<functional>
#include<vector>
#include<ctime>

using namespace std;

// using task_t function<void()>;
typedef function<void()> task_t;

void Download()
{
    cout << "执行下载任务" << " 通过子进程: "<< getpid() <<endl;
}

void PrintLog()
{
    cout << "执行打印日志任务" << " 通过子进程: "<< getpid()<< endl;
}

void PushStream()
{
    cout << "执行传输数据流任务" << " 通过子进程: "<< getpid()<< endl;
}

class Init
{
public:
    Init()
    {
        tasks.push_back(Download);
        tasks.push_back(PushStream);
        tasks.push_back(PrintLog);

        srand(time(nullptr) ^ getpid());
    }
    bool CheckSafe(int code)
    {
        if(code >= 0 && code < tasks.size()) return true;
        else return false;
    }
    void RunTask(int code)
    {
        // tasks数组中存放着可调用对象，通过()调用
        return tasks[code]();
    }
    int SelectTask()
    {
        return rand() % tasks.size();
    }

private:

    // 任务列表
    vector<task_t> tasks;
    // 任务码
    const int download_code = 0;
    const int print_code = 1;
    const int push_stream_code = 2;
};
// 定义全局对象
Init init;